<template>
  <!-- 对话框 -->
  <el-dialog @close="btnCancel" title="新增员工" :visible="showDialog">
    <!-- 表单 -->
    <el-form
      ref="addEmployee"
      :model="formData"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item prop="username" label="姓名：">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="mobile" label="手机号：">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间：">
        <el-date-picker
          v-model="formData.timeOfEntry"
          type="date"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式：">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <!-- 遍历只能遍历组件的数据 -->
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号：">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="departmentName" label="部门：">
        <el-input
          @focus="getDepartments"
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
        ></el-input>
        <el-tree
          v-if="showTree"
          :data="treeData"
          v-loading="loading"
          :props="{ label: 'name' }"
          @node-click="selectNode"
        ></el-tree>
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间：">
        <el-date-picker
          v-model="formData.correctionTime"
          type="date"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button @click="btnCancel" size="small">取消</el-button>
        <el-button @click="btnOk" size="small" type="primary">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { addEmployee } from "@/api/employees";
import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils";
import EmployeeEnum from "@/api/constant/employees";

export default {
  //组件名称
  name: "",
  //import引入的组件需要注入到对象中才能使用
  components: {},
  //父传子数据传递 父组件通过绑定属性来传递
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    //这里存放数据
    return {
      EmployeeEnum,
      // 表单数据 要提交给后端 所以和接口文档的名字一样
      formData: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      loading: false, //加载状态
      treeData: [], //树形数据
      showTree: false, //是否显示树形组件
      rules: {
        username: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" },
          {
            min: 1,
            max: 4,
            message: "用户姓名为1-4位",
          },
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        timeOfEntry: [{ required: true, message: "入职时间", trigger: "blur" }],
        correctionTime: [
          { required: true, message: "转正时间", trigger: "blur" },
        ],
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    // 获取部门数据
    async getDepartments() {
      this.loading = true;
      this.showTree = true;
      const { depts } = await getDepartments();
      this.treeData = tranListToTreeData(depts, "");
      this.loading = false;
    },
    // 选择部门
    selectNode(node) {
      // 树形和输入框联动数据
      this.formData.departmentName = node.name;
      //   关闭树形结构组件
      this.showTree = false;
    },
    // 确定
    async btnOk() {
      try {
        // 验证表单
        await this.$refs.addEmployee.validate();
        // 验证通过 新增员工接口
        await addEmployee(this.formData);
        // this.$parent //父组件实例 包裹该组件的
        this.$parent.getEmployeeList(); //重新获取数据，视图更新
        this.$message.success("新增员工成功");
        this.$parent.showDialog = false;
      } catch (err) {
        console.log(err);
      }
    },
    // 取消
    btnCancel(){
        this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.addEmployee.resetFields() // 重置校验结果
      this.$emit('update:showDialog', false)
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style  scoped>
</style>